var mongoose = require('mongoose');
// 数据库连接地址
var dbUrl = 'mongodb://localhost:27017/myapp';
//数据库连接
mongoose.connect(dbUrl);

// 数据库连接失败
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
// 数据库连接成功
mongoose.connection.once('open', function () {
  console.log('mongoose connection open to ' + dbUrl);
});
//连接断开终端显示消息
mongoose.connection.on('disconnected', function () {
  console.log('mongoose disconnected');
});
// exports.mongoose = mongoose;
var Schema = mongoose.Schema;

var userSchema = new Schema({
  userName: String,
  userInfo: {
    age: Number,
    sex: String,
  },
  userHobby: [String, String, String]
}, { collection: 'users' });

var userModel = mongoose.model("User", userSchema);
// var users = function () { };
// 保存
// users.prototype.create = function (docs, callback) {
//   this.create(docs, function (err) {
//     callback(err);
//   });
// };
// 按用户名精确查询
// users.prototype.findByName = function (userName, callback) {
//   userModel.findOne({ userName: userName }, function (obj, err) {
//     callback(err, obj);
//   });
// };

module.exports = userModel;